//
//  HEHealthAssesViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/21.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEHealthAssesViewController.h"
#import "HEAssesListModel.h"
#import "HEHealthAssesCell.h"
#import "HEWebHealthAssesViewController.h"

@interface HEHealthAssesViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, copy) NSMutableArray<HEAssesListModel *> *dataSourceArr;

@property (nonatomic, strong) GDTableView *tableView;


@end

@implementation HEHealthAssesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"健康评估";
    
    _dataSourceArr = @[].mutableCopy;
    
    [self initUI];
    [self initHeaderView];
    [self loadData];
}

- (void)loadData {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KScaleInfoUrl parameters:@{} success:^(id  _Nonnull responseObject) {
        [self.tableView.mj_header endRefreshing];

        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        
        [self.dataSourceArr removeAllObjects];
        
        [self.dataSourceArr addObjectsFromArray:[HEAssesListModel mj_objectArrayWithKeyValuesArray:response.data]];
          
        [self.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
 
- (void)initUI {
   _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStylePlain];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    
   _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   _tableView.scrollsToTop = YES;
   _tableView.tableFooterView = [[UIView alloc] init];
    
    [_tableView registerClass:HEHealthAssesCell.class forCellReuseIdentifier: @"HEHealthAssesCell"];
   
   self.tableView.backgroundColor = CLightGrayColor;
   self.tableView.delegate = self;
   self.tableView.dataSource = self;
   [self.view addSubview:self.tableView];
    
    __weak __typeof(&*self)weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.isFromHeadRefresh = YES;
        [weakSelf loadData];
    }];
}

- (void)initHeaderView {
    
    UIView *warp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 130)];;
    warp.backgroundColor = CViewBgColor;
    
    UIImageView *headImage = [UIImageView new];
    headImage.contentMode = UIViewContentModeScaleAspectFill;
    headImage.clipsToBounds = YES;
    headImage.layer.cornerRadius = 12;
    headImage.layer.masksToBounds = YES;
    [headImage sd_setImageWithURL:[NSURL URLWithString:@"https://www.honglutec.com/WechatApplet/static/image/health/report.png"] placeholderImage:IMAGE_NAMED(@"noImage")];
    [warp addSubview:headImage];
    
    headImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toAssesHisClick)];
    [headImage addGestureRecognizer:tap];
    
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(warp).offset(10);
        make.right.equalTo(warp).offset(-10);
        make.bottom.equalTo(warp).offset(-0);
    }];
     
    self.tableView.tableHeaderView = warp;
}

#pragma mark -
- (void)toAssesHisClick {
    NSBundle *bundle = [NSBundle mainBundle];
        NSString *resPath = [bundle resourcePath];
        NSString *filePath = [resPath stringByAppendingPathComponent:@"/H5/form/index.html"];
    NSURL * file = [NSURL fileURLWithPath:filePath];
  
    NSString *loclUrl = [NSString stringWithFormat:@"%@?pagetype=2&token=%@&height=%f&url=%@/oldman",file.absoluteString,[UserManager sharedUserManager].getToken, kNavBarHeight,KServerUrl];
 
    XLWebViewController *webView = [[XLWebViewController alloc] init];
    webView.url = loclUrl;
    [webView showNav:NO];
    [self.navigationController pushViewController:webView animated:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEHealthAssesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HEHealthAssesCell"];
    cell.model = _dataSourceArr[indexPath.row];
   return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 
   return [tableView fd_heightForCellWithIdentifier: @"HEHealthAssesCell" cacheByIndexPath:indexPath configuration:^(HEHealthAssesCell *cell) {
       cell.model = self.dataSourceArr[indexPath.row];
   }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEAssesListModel *model = _dataSourceArr[indexPath.row];
    
    NSBundle *bundle = [NSBundle mainBundle];
        NSString *resPath = [bundle resourcePath];
        NSString *filePath = [resPath stringByAppendingPathComponent:@"/H5/form/index.html"];
    NSURL * file = [NSURL fileURLWithPath:filePath];
  
    NSString *loclUrl = [NSString stringWithFormat:@"%@?pagetype=1&questiontype=%@&token=%@&height=%f&url=%@/oldman&reference=%@",file.absoluteURL, model.check_code,[UserManager sharedUserManager].getToken, kNavBarHeight,KServerUrl, [model.check_reference stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];

    XLWebViewController *webView = [[XLWebViewController alloc] init];
    webView.url = loclUrl;
    [webView showNav:NO];
    [self.navigationController pushViewController:webView animated:YES];
}

@end
